<%@page import="java.util.GregorianCalendar"%>
<%@page import="java.util.Date"%>
<%@page import="com.mongodb.BasicDBList"%>
<%@page import="com.mongodb.BasicDBObject"%>
<%@page import="com.mongodb.DBObject"%>
<%@page import="com.mongodb.DBCursor"%>
<%@page import="com.mongodb.DBCollection"%>

<%@ page import="devboard.dbutil.MongoUtil" %>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
	<head>
		<link rel="icon" type="image/png" href="favicon.png" >
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>devboard</title>
		<link rel="stylesheet" href="css/Global.css" type="text/css">
		<link rel="stylesheet" href="css/Board.css" type="text/css">
		<script type="text/javascript" src="js/Board.js"></script>
	</head>
	<body onload="init()">
		<%
		DBCollection tasks=MongoUtil.getCollection("Tasks");
		DBCollection users=MongoUtil.getCollection("Users");
		DBCursor cursor=null;
		%>
		<div id="content">
			<table>
				<tr>
					<th>project id</th><th>team</th><th>task name</th><th>reproduced</th><th>fixed</th><th>delivered</th>
					<th>consumed</th><th>developer</th><th><div class="tadi"><img src="css/plus.png"  id="addProject" width="30" height="30" onclick="showSingleAdd(event)"></div></th>
				</tr>
					<%
					BasicDBObject dbo=new BasicDBObject();
					//filter project
					String[] projectsSel=request.getParameterValues("projectSelect");
					if((projectsSel!=null)&&(!projectsSel[0].equals("All"))){
						BasicDBList temp=new BasicDBList();
						for(String s:projectsSel){
							temp.add(new BasicDBObject("project", s));
						}
						dbo.put("$or", temp);
					}
					//filter date
					
					if(request.getParameter("fromDate")!=null){
										
						String[] to=request.getParameter("toDate").split("\\.");
						String[] from=request.getParameter("fromDate").split("\\.");
						GregorianCalendar fromDate=new GregorianCalendar(Integer.parseInt(from[2]),
								Integer.parseInt(from[1])-1,Integer.parseInt(from[0]));
						GregorianCalendar toDate=new GregorianCalendar(Integer.parseInt(to[2]),
								Integer.parseInt(to[1])-1,Integer.parseInt(to[0])+1);
						BasicDBList temp=new BasicDBList();
						temp.add(new BasicDBObject("timeCreation", new BasicDBObject("$gt",fromDate.getTimeInMillis())));
						temp.add(new BasicDBObject("timeCreation", new BasicDBObject("$lt",toDate.getTimeInMillis())));
						dbo.put("$and", temp);
						
					}
					//filter status
					if((request.getParameter("boolSelect")!=null)&&(!request.getParameter("boolSelect").equals("all"))){
						dbo.put(request.getParameter("boolSelect"), "yes");
					}
					
					//filter developer
					if((request.getParameter("developerSelect")!=null)&&(!request.getParameter("developerSelect").equals("all"))){
						dbo.put("userId", request.getParameter("developerSelect"));
					}
					
					//find query
					if((request.getParameter("queryFind")!=null)&&(!request.getParameter("queryFind").equals(""))){
						BasicDBObject temp=new BasicDBObject();
						temp.put("$regex", request.getParameter("queryFind"));
						temp.put("$options", "i");
						BasicDBList tempList=new BasicDBList();
						tempList.add(new BasicDBObject("projectId", temp));
						tempList.add(new BasicDBObject("name", temp));
						dbo.put("$or", tempList);
					}
					
					cursor=tasks.find(dbo).sort(new BasicDBObject("timeCreation",-1));
					while(cursor.hasNext()){
						DBObject task=cursor.next();%>
						<tr>
							<td>
								<a  id="projectId_<%=task.get("_id") %>" 
										href="http://webeeh.gdev.com/projects/issue_tracker_projects/Project%20<%=task.get("projectId") %>?object_id=<%=task.get("projectId") %>&project_type=Project"
											target="_blank" style="font-weight:bold;"><%=task.get("projectId") %></a>
								<a  id="release_<%=task.get("_id") %>" 
										href="http://webeeh.gdev.com/projects/issue_tracker_projects/Release%20<%=task.get("release") %>?object_id=<%=task.get("release") %>&project_type=Release" 
											target="_blank" style="font-size:8pt;"><%=task.get("release") %></a>
							</td>
							<td><b><%=task.get("project")  %></b><br /><span style="font-size:8pt;"><%=task.get("serviceType")  %></span></td>
							<td id="<%=task.get("_id") %>" onmouseout="hideEditButton(this)" onmouseover="showEditButton(this)">
								<a id="name_link_<%=task.get("_id") %>" href="<%=task.get("link") %>" target="_blank"><%=task.get("name") %></a>
								<button class="editButton" onclick="showEditMenu(this, '<%=task.get("_id") %>', '<%=task.get("userId") %>', event)">edit</button>
							</td>
							<td>
								<div class="tadi" onclick="changeStatus(this, 'reproduced', '<%=task.get("_id") %>', event)">
									<img src="css/<%=task.get("reproduced") %>.png" width="30" height="30">
								</div>
							</td>
							<td>
								<div class="tadi" onclick="changeStatus(this, 'inProgress', '<%=task.get("_id") %>', event)">
									<img src="css/<%=task.get("inProgress") %>.png" width="30" height="30">
								</div>
							</td>
							<td>
								<div class="tadi" onclick="changeStatus(this,'delivered','<%=task.get("_id") %>', event)">
									<img src="css/<%=task.get("delivered") %>.png" width="30" height="30" onmouseover="showDeliverInfo('<%=task.get("_id") %>', event, this)">
								</div>
							</td>
							<td>
								<div class="tadi" onclick="changeStatus(this,'successfully','<%=task.get("_id") %>', event)">
									<img src="css/<%=task.get("successfully") %>.png" width="30" height="30">
								</div>
							</td>
							<td id="userID_<%=task.get("_id") %>">
								<%
								if(!task.get("userId").equals("nobody")){
									DBObject user=users.findOne(new BasicDBObject().append("_id", task.get("userId")));
									out.print(user.get("firstName")+" "+user.get("lastName"));
								}else{
									out.print("Mr. Nobody");
								}
								%>
							</td>
							<td id="pay_<%=task.get("_id") %>" onclick="unpaid('<%=task.get("_id") %>')">
								<%if("yes".equals(task.get("paid"))){ %>
									<img src="css/coins.png" width="30">
								<%} %>
							</td>
						</tr>
					<%} %>
			</table>
			
			<div id="editMenu" class="Popup">
				<div id="editMenuMain">
					<input id="editID" type="hidden" value="">
					<table>
					<tr><td>name:</td><td><input id="editName" name="editName" type="text"></td></tr>
					<tr><td>link:</td><td><input id="editLink" name="editLink" type="text"></td></tr>
					<tr><td>project id:</td><td><input id="editProjectId" name="editProjectId" type="text"></td></tr>
					<tr><td>release:</td><td><input id="editRelease" type="text"></td></tr>
					<tr><td>developer:</td><td><select id="editSelect">
					<%
					cursor=users.find(new BasicDBObject("role","developer"));
					while(cursor.hasNext()){
						DBObject user=cursor.next();
						out.println("<option value=\""+user.get("_id")+"\" >"+user.get("firstName")+" "+user.get("lastName")+"</option>");
					}
					%>
						</select></td></tr>
					</table>
					<button onclick="deleteTaskData()">delete</button>
					<button onclick="updateTaskData()">save</button>
					<button onclick="closePopup(editMenu)" >close</button>
					<button onclick="showIssueEditor()">create issue</button>
					<button id="pay_button" onclick="payForJob()">pay</button>
				</div>
				<div id="issueEditor">
					<div id="textArea"><textarea id="issueText"></textarea></div>
					<div><button onclick="updateIssue()">ok</button> <button onclick="hideIssueEditor()">cancel</button></div>
				</div>		
			</div>
			
			
			
			
			<div id="singleAdd" data-id="" class="Popup">
				<table>
					<tr><td>project id:</td><td><input id="singleAddProjectId" name="singleAddProjectId" type="text"></td></tr>
					<tr><td>team:</td><td><input id="singleAddTeam" name="singleAddTeam" type="text"></td></tr>
					<tr><td>name:</td><td><input id="singleAddName" name="singleAddName" type="text"></td></tr>
					<tr><td>link:</td><td><input id="singleAddLink" name="singleAddLink" type="text"></td></tr>					
				</table>
				<button onclick="singleTaskAdd()">ok</button>
				<button onclick="closePopup(singleAddPopup)">close</button>
			</div>
			
			
			
			<div id="editIssue" data-id="" class="Popup" data-name="">
				<textarea id="issueContent"></textarea>
				<div>
					<button onclick="resolveIssue()">resolve</button>
					<button onclick="closePopup(editIssuePopup)">close</button>
					<button onclick="">attach a files</button>
				</div>
			</div>
			
			<div id="deliverInfo" class="Popup">
			</div>
			
			
			
			
		</div>
	</body>
</html>